package com.gdut.simple.view.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.gdut.simple.R;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.contract.LoginContract;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UserEvent;
import com.gdut.simple.presenter.user.LoginPresenter;
import com.gdut.simple.view.basic.AbsBasicActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

public class LoginActivity extends AbsBasicActivity implements LoginContract.View {

    private static final String TAG = LoginActivity.class.getSimpleName();

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_login_acc_text)
    EditText mAccEditText;
    @BindView(R.id.id_login_psw_text)
    EditText mPswEditText;
    @BindView(R.id.id_login_button)
    Button mLoginButton;

    @OnClick(R.id.id_login_button)
    void login() {
        mLoginPresenter.login();
    }

    private LoginContract.Presenter mLoginPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initToolbar();
        new LoginPresenter(this);
        mSubscription = RxBus.getDefault().toObservable(UserEvent.class).subscribe(new Action1<UserEvent>() {
            @Override
            public void call(UserEvent event) {
                if (event.isOnline()) {
                    LogUtils.d(TAG, "receive LoginEvent");
                    finish();
                }
            }
        });
    }

    private void initToolbar() {
        mToolbar.setTitle(R.string.user_login);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    public String getAccount() {
        return mAccEditText != null ? mAccEditText.getText().toString().trim() : "";
    }

    @Override
    public String getPassword() {
        return mPswEditText != null ? mPswEditText.getText().toString().trim() : "";
    }

    @Override
    public void next() {
        RxBus.getDefault().post(new UserEvent(true));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 100);
    }

    @Override
    protected void onDestroy() {
        if (mLoginPresenter != null) {
            mLoginPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void setPresenter(@NonNull LoginContract.Presenter presenter) {
        this.mLoginPresenter = presenter;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_register) {
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        }
        return true;
    }
}
